package com.stefan.stack;


import java.util.Scanner;
/*
* 思路，数组的尾插法和尾取法，是天然的栈结构
* */
public class ArrayStefanStack<E> implements StefanStack<E>{
    private Object[] data;
    private int size;
    private int capacity;

    public ArrayStefanStack(int capacity) {
        this.capacity = capacity;
        this.data = new Object[capacity];
    }

    @Override
    public void push(E e) {
        resize();
        data[size++] = e; //插入数组最后
    }

    @Override
    public E pop() {
        resize();
        E pop = (E) data[--size];
        data[size] = null;
        return pop;
    }

    @Override
    public int size() {
        return this.size;
    }
    private void resize() {
        if (capacity > 10) {
            int a ;
            if (size < (a = capacity>>1)) {
                capacity = a; // 缩容
                Object[] resize = new Object[capacity];
                for (int i = 0; i < size; i++) {
                    resize[i] = data[i];
                }
                data = resize;
            }
        } else {
            //扩容
            if (size > capacity-1) {
                capacity = capacity<<1;//扩容2倍
                Object[] resize = new Object[capacity];
                for (int i = 0; i < size; i++) {
                    resize[i] = data[i];
                }
                data = resize;
            }
        }


        if (data == null) {
            capacity = 10;
            data = new Object[capacity];
        } else {

        }
    }
    public boolean isEmpty() {
        return this.size == 0;
    }

    public static int calculator (String express) {
        // example 1+16*2-8
        //用栈来实现一个运算比如 3+18*1-10+9-18/
        //思路用两个栈，一个栈数字一个栈放符号
        StefanStack<Integer> num = new ArrayStefanStack<>(10);
        StefanStack<Character> cal = new ArrayStefanStack<>(10);
        char[] charArray = express.toCharArray();
        char priority = '\u0000';
        for (char c : charArray) {
            if (calculaterMatcher(c)) {
                if (priority(c)) {
                    priority = c;
                }
                cal.push(c);
            } else {
                num.push(Integer.valueOf(String.valueOf(c)));
                if (priority != '\u0000') {
                    Character pop = cal.pop();
                    int a = 0;
                    int after = num.pop();
                    int before = num.pop();
                    switch (pop) {
                        case '+': a = before + after;break;
                        case '-': a = before - after;break;
                        case '*': a = before * after;break;
                        case '/':
                            if (after == 0) {
                                throw new ArithmeticException("除数不能为0");
                            }
                            a = before / after;
                            break;
                    }
                    num.push(a);
                   priority = '\u0000';
                }
            }
        }
        if (num.size() == 1 && cal.isEmpty()) {
            return num.pop();
        } else {
            do {
                Character pop = cal.pop();
                int a = 0;
                int after = num.pop();
                int before = num.pop();
                switch (pop) {
                    case '+': a = before + after;break;
                    case '-': a = before - after;break;
                    case '*': a = before * after;break;
                    case '/':
                        if (after == 0) {
                            throw new ArithmeticException("除数不能为0");
                        }
                        a = before / after;
                        break;
                }
                num.push(a);
            } while (!cal.isEmpty());
        }
        return  num.pop();
    }

    public static boolean calculaterMatcher(char c) {
        switch (c) {
            case '+':
            case '-':
            case '*':
            case '/':
                return true;
            default: return false;
        }
    }
    public static boolean priority(char c) {
        switch (c) {
            case '+':
            case '-':
                return false;
            case '*':
            case '/':
                return true;
            default: return false;
        }
    }

    public static void main(String[] args) {
        StefanStack<Character> sb = new ArrayStefanStack<>(10);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.next();
            System.out.println(sb.charsMatcher(s));
//            System.out.println(calculator(s));
        }

    }
}
